import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login'
import Home from '../components/Home'
import Welcome from '../components/Welcome'
import Users from '../components/user/Users'
import Report from '../components/report/Report'
import Rights from '../components/power/Rights'
import Roles from '../components/power/Roles'
import MyMap from '../components/map/MyMap'
import MyVuex from '../components/vuex/state'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [{
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [{
          path: '/welcome',
          component: Welcome
        }, {
          path: '/users',
          component: Users
        }, {
          path: '/reports',
          component: Report
        }, {
          path: '/rights',
          component: Rights
        }, {
          path: '/roles',
          component: Roles
        }, {
          path: '/map',
          component: MyMap
        },
        {
          path: '/vuex',
          component: MyVuex
        }
      ]
    }
  ]
})

router.beforeEach((
  to, from, next
) => {
  // to 要去那个路径
  // from  从哪个路径来
  // next 1.放行 2.跳转那一页

  //访问登录页直接放行
  if (to.path == '/login') return next()
  //获取token
  let token = window.sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})
// 挂载大屏
export default router
